package supercafe.core;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class XMLHandler {
	
	public static String getCharacterDataFromElement(Element e) {
	    Node child = e.getFirstChild();
	    if (child instanceof CharacterData) {
	      CharacterData cd = (CharacterData) child;
	      return cd.getData();
	    }
	    return "";
	  }
	  
		// return "null" if successed, else return message
	
	  public static String getMessage(String xmlResponse){
		  try{
			  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			  InputSource is = new InputSource();
			  is.setCharacterStream(new StringReader(xmlResponse));
			  Document doc = db.parse(is);
			  NodeList nodes1 = doc.getElementsByTagName("status");
			  String status = getCharacterDataFromElement((Element)nodes1.item(0));
			  String message = "Đã gữi dữ liệu thành công!";
			  if(Integer.parseInt(status)==0){
				  NodeList nodes2 = doc.getElementsByTagName("message");
				  message = getCharacterDataFromElement((Element)nodes2.item(0));
				  return message;
			  }
			  //System.out.println("status :"+status);
			  //System.out.println("message :"+message);
		  }catch(Exception e)
		  {			  
		  }
		  return null;
	  }
	  
	  public static void getMessage(Activity context,String xmlResponse){
		  try{
			  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			  InputSource is = new InputSource();
			  is.setCharacterStream(new StringReader(xmlResponse));
			  Document doc = db.parse(is);
			  NodeList nodes1 = doc.getElementsByTagName("status");
			  String status = getCharacterDataFromElement((Element)nodes1.item(0));
			  String message = "Đã gữi dữ liệu thành công!";
			  if(Integer.parseInt(status)==1){
				  NodeList nodes2 = doc.getElementsByTagName("message");
				  message = getCharacterDataFromElement((Element)nodes2.item(0));
			  }
			  Toast.makeText(context, message, Toast.LENGTH_LONG);
			  
		  }catch(Exception e)
		  {			  
		  }
	  }
	  
	  public static Vector<OrderItem> getListItems(Activity context,String xmlResponse, Vector<OrderItem> listItems){
		  /*format:
		   * <?xml version="1.0" encoding="UTF-8"?>
			<order id=":id">
				<employee>:id</employee>
				<table>:id</table>
				<items>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					.....
				</items>
				<price>:price</price>
				<prepared>:prepared</prepared>
			</order>
		   	*/
		  try{
			  if(xmlResponse.equals("null")){
				  //Toast.makeText(context, "Có lỗi! Không tìm thấy đơn hàng hoặc không có dữ liệu.", Toast.LENGTH_LONG);
			  }
			  else {
				  Log.e("@nhannnnnnnn", xmlResponse);
				  //Toast.makeText(context, xmlResponse, Toast.LENGTH_LONG);
				  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				  InputSource is = new InputSource();
				  is.setCharacterStream(new StringReader(xmlResponse));
				  Document doc = db.parse(is);
				  
				  //NodeList nodeOrder = doc.getElementsByTagName("order");
				  //get employee id
				  NodeList nodeEmployeeId = doc.getElementsByTagName("employeeId");
				  int employeeId = Integer.parseInt(getCharacterDataFromElement((Element)nodeEmployeeId.item(0)));
				//get employee name
				  NodeList nodeEmployeeName = doc.getElementsByTagName("employeeName");
				  String employeeName = (getCharacterDataFromElement((Element)nodeEmployeeName.item(0)));
				  
				  //get table id
				  NodeList nodeTable = doc.getElementsByTagName("tableId");
				  int tableId = Integer.parseInt(getCharacterDataFromElement((Element)nodeTable.item(0)));
				  //get totalPrice
				  NodeList nodeTotalPrice = doc.getElementsByTagName("totalPrice");
				  int totalPrice = Integer.parseInt(getCharacterDataFromElement((Element)nodeTotalPrice.item(0)));
				  //get prepared
				  NodeList nodePrepared = doc.getElementsByTagName("prepared");
				  int prepared = Integer.parseInt(getCharacterDataFromElement((Element)nodePrepared.item(0)));
				  //get item(s)
				  NodeList nodeItems = doc.getElementsByTagName("item");
				  int numOfItems = nodeItems.getLength();
				  
				  String message = "";
				  for(int i=0;i<numOfItems;i++){
					  Element item = (Element) nodeItems.item(i);
					  int itemId = Integer.parseInt(item.getAttribute("id"));
					  
					  NodeList name = item.getElementsByTagName("itemName");
					  String itemName = (getCharacterDataFromElement((Element)name.item(0)));
					  
					  NodeList quan = item.getElementsByTagName("quanlity");
					  int quanlity = Integer.parseInt(getCharacterDataFromElement((Element)quan.item(0)));
					  
					  NodeList pri = item.getElementsByTagName("itemPrice");
					  int price = Integer.parseInt(getCharacterDataFromElement((Element)pri.item(0)));
					  
					  NodeList noteNL = item.getElementsByTagName("note");
					  String note = (getCharacterDataFromElement((Element)noteNL.item(0)));
					  				  
					  listItems.add(new OrderItem(itemId, itemName, quanlity, price, note));
					  message += itemName+"\n";
				  }
				  
				  
				  //Toast.makeText(context, message, Toast.LENGTH_LONG);
			  }			  
		  }catch(Exception e)
		  {			  
			  e.printStackTrace();
		  }
		  return listItems;
	  }
	  
	  public static String getEmployeeName(String xmlResponse){
		  /*format:
		   * <?xml version="1.0" encoding="UTF-8"?>
			<order id=":id">
				<employee>:id</employee>
				<table>:id</table>
				<items>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					.....
				</items>
				<price>:price</price>
				<prepared>:prepared</prepared>
			</order>
		   	*/
		  String employeeName = null;
		  try{
			  if(xmlResponse.equals("null")){
				  //Toast.makeText(context, "Có lỗi! Không tìm thấy đơn hàng hoặc không có dữ liệu.", Toast.LENGTH_LONG);
			  }
			  else {
				  Log.e("@nhannnnnnnn", xmlResponse);
				  //Toast.makeText(context, xmlResponse, Toast.LENGTH_LONG);
				  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				  InputSource is = new InputSource();
				  is.setCharacterStream(new StringReader(xmlResponse));
				  Document doc = db.parse(is);
				  
				//get employee name
				  NodeList nodeEmployeeName = doc.getElementsByTagName("employeeName");
				  employeeName = (getCharacterDataFromElement((Element)nodeEmployeeName.item(0)));
				  
			  }			  
		  }catch(Exception e)
		  {			  
			  e.printStackTrace();
		  }
		  return employeeName;
	  }
	  
	  public static int getTotalPrice(String xmlResponse){
		  /*format:
		   * <?xml version="1.0" encoding="UTF-8"?>
			<order id=":id">
				<employee>:id</employee>
				<table>:id</table>
				<items>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					<item id=":id">
						<quanlity>:quanlity</quanlity>
						<itemPrice>:toltalItemPrice</itemPrice>
						<note>:note</note>
					</item>
					.....
				</items>
				<price>:price</price>
				<prepared>:prepared</prepared>
			</order>
		   	*/
		  try{
			  if(xmlResponse.equals("null")){
				  //Toast.makeText(context, "Có lỗi! Không tìm thấy đơn hàng hoặc không có dữ liệu.", Toast.LENGTH_LONG);
				  return 0;
			  }
			  else {
				  Log.e("@nhannnnnnnn", xmlResponse);
				  //Toast.makeText(context, xmlResponse, Toast.LENGTH_LONG);
				  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				  InputSource is = new InputSource();
				  is.setCharacterStream(new StringReader(xmlResponse));
				  Document doc = db.parse(is);
				  
				  //get totalPrice
				  NodeList nodeTotalPrice = doc.getElementsByTagName("totalPrice");
				  int totalPrice = Integer.parseInt(getCharacterDataFromElement((Element)nodeTotalPrice.item(0)));
				  return totalPrice;
			  }			  
		  }catch(Exception e)
		  {			  
			  e.printStackTrace();
		  }
		  return 0;
	  }

	public static Vector<MenuType> getListMenus(
			Activity activity, String xmlResponse) {
		// TODO Auto-generated method stub
		Vector<MenuType> listMenus = null;
		try{
			  if(xmlResponse.equals("null")){
				  //Toast.makeText(activity, "Có lỗi! Không tìm thấy thực đơn hoặc không có dữ liệu.", Toast.LENGTH_LONG);
			  }
			  else {
				  Log.e("@nhannnnnnnn", xmlResponse);
				  //Toast.makeText(context, xmlResponse, Toast.LENGTH_LONG);
				  DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				  InputSource is = new InputSource();
				  is.setCharacterStream(new StringReader(xmlResponse));
				  Document doc = db.parse(is);
				  
				  NodeList nodeTypes = doc.getElementsByTagName("type");
				  int numOfTypes = nodeTypes.getLength();
				  
				  listMenus = new Vector<MenuType>();
				  String message = "";
				  for(int i=0;i<numOfTypes;i++){
					  Element type = (Element) nodeTypes.item(i);
					  int typeId = Integer.parseInt(type.getAttribute("id"));
					  String typeName = (type.getAttribute("name"));
					  
					  NodeList nodeItems = type.getElementsByTagName("item");
					  int numOfItems = nodeItems.getLength();
					  
					  ArrayList<MenuItem> listItems = new ArrayList<MenuItem>();
					  for(int k=0;k<numOfItems;k++){
						  Element item = (Element) nodeItems.item(k);
						  
						  int itemId = Integer.parseInt(item.getAttribute("id"));
						  
						  NodeList nameNL = item.getElementsByTagName("name");
						  String itemName = (getCharacterDataFromElement((Element)nameNL.item(0)));
						  
						  NodeList priceNL = item.getElementsByTagName("price");
						  int itemPrice = Integer.parseInt(getCharacterDataFromElement((Element)priceNL.item(0)));
						  
						  listItems.add(new MenuItem(itemId,itemName,itemPrice));
					  }
					  
					  listMenus.add(new MenuType(typeId, typeName, listItems));
				  }
				  //Toast.makeText(context, message, Toast.LENGTH_LONG);
			  }			  
		  }catch(Exception e)
		  {			  
			  e.printStackTrace();
		  }
		  
		  return listMenus;
	}
}
